#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import traceback

from pyzabbix import ZabbixAPI
import time
import sys

if sys.version_info[0] == 2:
    reload(sys)
    sys.setdefaultencoding('utf-8')


zabbix_server = 'http://192.168.50.102/zabbix'


kafka_server = '192.168.60.114:9092'
kafka_topic = 'aaa'
zabbix_username = 'Admin'
zabbix_password = 'zabbix'



def send_alarm_to_kafka(app, eventType, eventId, alarmCreateTime, priority, alarmContent, resourceName):
    alarm_time = alarmCreateTime * 1000
    producer = KafkaProducer(bootstrap_servers=kafka_server)
    event_data = [{
        "alarmTitle": 'zabbix_alarm',
        "app": app,  # 第三方平台名称
        "eventType": eventType,  # 触发告警trigger，关闭告警resolve
        "eventId": eventId,  # eventid
        "alarmCreateTime": alarm_time,  # 告警事件产生时间
        "priority": priority,  # 告警级别
        "alarmContent": alarmContent,  # 告警信息
        "alarmValue": '',
        "resourceName": resourceName,  # 资源名称
        "resourceIp": "",
        "details": "",
        "contexts": "",
        "metric": "",
        "originalData": "",
        "entityId": "",
        " notifyUrl ": ""
    }]
    print(event_data)
    future = producer.send(kafka_topic, key='zabbix_test', value=json.dumps(event_data).encode('utf-8'))

    try:
        future.get(timeout=3)
    except kafka_errors:
        traceback.format_exc()

def get_zabbix_event(zabbix_server,zabbix_username,zabbix_password):
    Zapi = ZabbixAPI(zabbix_server)
    Zapi._login(zabbix_username, zabbix_password)
    #主机
    host_list = Zapi.host.get(
        output="extend",
    )
    app_name = str(zabbix_server.split('/')[2]) + '_zabbix'


    for host in host_list:
        hostid = host['hostid']
        events = Zapi.event.get(
            hostids=hostid,
        )
        if events:
            for event in events:
                old_time = int(time.time()) - 300
                if int(event['clock']) > old_time:
                    # print json.dumps(event)
                    print(json.loads('"%s"' % json.dumps(event)))
                    #send_alarm_to_kafka(app=app_name,eventType='trigger',eventId=str(event['eventid']),alarmCreateTime=int(event['clock']),priority='2',alarmContent=str(event['name']))

def get_zabbix_alert(zabbix_server,zabbix_username,zabbix_password):
    Zapi = ZabbixAPI(zabbix_server)
    Zapi._login(zabbix_username, zabbix_password)
    #主机
    host_list = Zapi.host.get(
        output="extend",
    )
    app_name = str(zabbix_server.split('/')[2]) + '_zabbix'


    for host in host_list:
        hostid = host['hostid']
        alerts = Zapi.alert.get(
            hostids=hostid,
        )
        if alerts:
            for event in alerts:
                print(json.dumps(event))




if __name__ in "__main__":
    app_name='zabbix'
    ResourceName = 'CWDM_10.17.1.50'
    # alarmValue = '0.7 dB'
    get_zabbix_alert(zabbix_server,zabbix_username,zabbix_password)
    send_alarm_to_kafka(app=app_name, eventType='trigger', eventId='9999998',alarmCreateTime=int(time.time()), priority='4', alarmContent=str('光纤光衰值已高于0.5dB!'),resourceName=ResourceName)

